#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include "WTSVariant.hpp"
#include <fstream>
#include <chrono>
#include <thread>
#include <vector>
#include <atomic>

using namespace wtp;

/*!
 * \brief WTSVariant工具类测试套件
 * 
 * \details 测试WTSVariant类的基本功能，包括：
 *          - 对象和数组的创建
 *          - 基本数据类型的存储和获取
 *          - 内存管理
 *          - 类型检查
 */
class WTSVariantTest : public ::testing::Test {
protected:
    void SetUp() override {
        // 测试前准备
    }
    
    void TearDown() override {
        // 测试后清理
    }
};

// ========== WTSVariant基础类型测试 ==========

/*!
 * \brief 测试WTSVariant对象和数组的创建
 */
TEST_F(WTSVariantTest, TestVariantCreationAndTypes) {
    // 测试对象创建
    WTSVariant* obj = WTSVariant::createObject();
    ASSERT_NE(obj, nullptr);
    EXPECT_TRUE(obj->isObject());
    EXPECT_FALSE(obj->isArray());
    EXPECT_EQ(obj->type(), WTSVariant::VT_Object);
    obj->release();

    // 测试数组创建
    WTSVariant* arr = WTSVariant::createArray();
    ASSERT_NE(arr, nullptr);
    EXPECT_TRUE(arr->isArray());
    EXPECT_FALSE(arr->isObject());
    EXPECT_EQ(arr->type(), WTSVariant::VT_Array);
    arr->release();
}

/*!
 * \brief 测试WTSVariant对象操作
 */
TEST_F(WTSVariantTest, TestObjectOperations) {
    WTSVariant* obj = WTSVariant::createObject();
    ASSERT_NE(obj, nullptr);

    // 添加不同类型的成员
    EXPECT_TRUE(obj->append("name", "WonderTrader"));
    EXPECT_TRUE(obj->append("version", 1));
    EXPECT_TRUE(obj->append("price", 99.99));
    EXPECT_TRUE(obj->append("active", true));

    // 验证成员存在
    EXPECT_TRUE(obj->has("name"));
    EXPECT_TRUE(obj->has("version"));
    EXPECT_TRUE(obj->has("price"));
    EXPECT_TRUE(obj->has("active"));
    EXPECT_FALSE(obj->has("nonexistent"));

    // 获取成员并验证类型和值
    WTSVariant* name_var = obj->get("name");
    ASSERT_NE(name_var, nullptr);
    EXPECT_EQ(name_var->type(), WTSVariant::VT_String);
    EXPECT_STREQ(name_var->asCString(), "WonderTrader");

    WTSVariant* version_var = obj->get("version");
    ASSERT_NE(version_var, nullptr);
    EXPECT_EQ(version_var->type(), WTSVariant::VT_Int32);
    EXPECT_EQ(version_var->asInt32(), 1);

    WTSVariant* price_var = obj->get("price");
    ASSERT_NE(price_var, nullptr);
    EXPECT_EQ(price_var->type(), WTSVariant::VT_Real);
    EXPECT_DOUBLE_EQ(price_var->asDouble(), 99.99);

    WTSVariant* active_var = obj->get("active");
    ASSERT_NE(active_var, nullptr);
    EXPECT_EQ(active_var->type(), WTSVariant::VT_Boolean);
    EXPECT_TRUE(active_var->asBoolean());

    obj->release();
}

/*!
 * \brief 测试WTSVariant数组操作
 */
TEST_F(WTSVariantTest, TestArrayOperations) {
    WTSVariant* arr = WTSVariant::createArray();
    ASSERT_NE(arr, nullptr);

    // 添加不同类型的元素
    EXPECT_TRUE(arr->append("first"));
    EXPECT_TRUE(arr->append(42));
    EXPECT_TRUE(arr->append(3.14));
    EXPECT_TRUE(arr->append(true));

    // 验证数组大小
    EXPECT_EQ(arr->size(), 4u);

    // 获取元素并验证
    WTSVariant* first_elem = arr->get(static_cast<uint32_t>(0));
    ASSERT_NE(first_elem, nullptr);
    EXPECT_EQ(first_elem->type(), WTSVariant::VT_String);
    EXPECT_STREQ(first_elem->asCString(), "first");

    WTSVariant* second_elem = arr->get(static_cast<uint32_t>(1));
    ASSERT_NE(second_elem, nullptr);
    EXPECT_EQ(second_elem->type(), WTSVariant::VT_Int32);
    EXPECT_EQ(second_elem->asInt32(), 42);

    WTSVariant* third_elem = arr->get(static_cast<uint32_t>(2));
    ASSERT_NE(third_elem, nullptr);
    EXPECT_EQ(third_elem->type(), WTSVariant::VT_Real);
    EXPECT_DOUBLE_EQ(third_elem->asDouble(), 3.14);

    WTSVariant* fourth_elem = arr->get(static_cast<uint32_t>(3));
    ASSERT_NE(fourth_elem, nullptr);
    EXPECT_EQ(fourth_elem->type(), WTSVariant::VT_Boolean);
    EXPECT_TRUE(fourth_elem->asBoolean());

    // 测试越界访问
    WTSVariant* out_of_bounds = arr->get(static_cast<uint32_t>(10));
    EXPECT_EQ(out_of_bounds, nullptr);

    arr->release();
}

/*!
 * \brief 测试WTSVariant嵌套结构
 */
TEST_F(WTSVariantTest, TestNestedStructures) {
    WTSVariant* root = WTSVariant::createObject();
    ASSERT_NE(root, nullptr);

    // 创建嵌套对象
    WTSVariant* config = WTSVariant::createObject();
    config->append("host", "localhost");
    config->append("port", 3306);
    root->append("database", config);

    // 创建嵌套数组
    WTSVariant* servers = WTSVariant::createArray();
    servers->append("server1");
    servers->append("server2");
    servers->append("server3");
    root->append("servers", servers);

    // 验证嵌套结构
    EXPECT_TRUE(root->has("database"));
    EXPECT_TRUE(root->has("servers"));

    WTSVariant* db_config = root->get("database");
    ASSERT_NE(db_config, nullptr);
    EXPECT_TRUE(db_config->isObject());
    EXPECT_TRUE(db_config->has("host"));
    EXPECT_TRUE(db_config->has("port"));

    WTSVariant* server_list = root->get("servers");
    ASSERT_NE(server_list, nullptr);
    EXPECT_TRUE(server_list->isArray());
    EXPECT_EQ(server_list->size(), 3u);

    root->release();
}

/*!
 * \brief 测试WTSVariant内存管理
 */
TEST_F(WTSVariantTest, TestMemoryManagement) {
    // 测试多次创建和释放
    for (int i = 0; i < 100; ++i) {
        WTSVariant* var = WTSVariant::createObject();
        ASSERT_NE(var, nullptr);
        
        // 添加一些数据
        var->append("index", i);
        var->append("data", "test_data");
        
        // 验证数据
        EXPECT_TRUE(var->has("index"));
        EXPECT_TRUE(var->has("data"));
        
        var->release();
    }
    
    // 如果没有内存泄漏，测试应该正常完成
    SUCCEED();
}

/*!
 * \brief 测试WTSVariant类型转换和便捷获取方法
 */
TEST_F(WTSVariantTest, TestTypeConversions) {
    WTSVariant* obj = WTSVariant::createObject();
    ASSERT_NE(obj, nullptr);
    
    // 添加不同类型的数据
    obj->append("int_val", 42);
    obj->append("double_val", 3.14159);
    obj->append("string_val", "test");
    obj->append("bool_val", true);
    
    // 测试便捷获取方法
    EXPECT_EQ(obj->getInt32("int_val"), 42);
    EXPECT_EQ(obj->getString("int_val"), "42");
    
    EXPECT_DOUBLE_EQ(obj->getDouble("double_val"), 3.14159);
    std::string double_str = obj->getString("double_val");
    EXPECT_TRUE(double_str.find("3.14") != std::string::npos);
    
    EXPECT_EQ(obj->getString("string_val"), "test");
    
    EXPECT_TRUE(obj->getBoolean("bool_val"));
    EXPECT_EQ(obj->getString("bool_val"), "true");
    
    // 测试不存在的键的默认值
    EXPECT_EQ(obj->getInt32("nonexistent"), 0);
    EXPECT_EQ(obj->getString("nonexistent"), "");
    EXPECT_FALSE(obj->getBoolean("nonexistent"));
    EXPECT_DOUBLE_EQ(obj->getDouble("nonexistent"), 0.0);
    
    obj->release();
}

/*!
 * \brief 测试WTSVariant边界条件
 */
TEST_F(WTSVariantTest, TestBoundaryConditions) {
    WTSVariant* obj = WTSVariant::createObject();
    ASSERT_NE(obj, nullptr);

    // 测试最大最小值
    obj->append("max_int", INT32_MAX);
    obj->append("min_int", INT32_MIN);
    obj->append("zero_int", 0);
    obj->append("zero_double", 0.0);

    EXPECT_EQ(obj->getInt32("max_int"), INT32_MAX);
    EXPECT_EQ(obj->getInt32("min_int"), INT32_MIN);
    EXPECT_EQ(obj->getInt32("zero_int"), 0);
    EXPECT_DOUBLE_EQ(obj->getDouble("zero_double"), 0.0);

    obj->release();
}

/*!
 * \brief 测试WTSVariant错误处理
 */
TEST_F(WTSVariantTest, TestErrorHandling) {
    WTSVariant* obj = WTSVariant::createObject();
    ASSERT_NE(obj, nullptr);

    // 测试获取不存在的键
    WTSVariant* nonexistent = obj->get("nonexistent_key");
    EXPECT_EQ(nonexistent, nullptr);

    // 测试在对象上使用数组方法
    WTSVariant* invalid_index = obj->get(static_cast<uint32_t>(0));
    EXPECT_EQ(invalid_index, nullptr);

    obj->release();

    // 测试在数组上使用对象方法
    WTSVariant* arr = WTSVariant::createArray();
    ASSERT_NE(arr, nullptr);

    EXPECT_FALSE(arr->has("key"));
    WTSVariant* invalid_key = arr->get("key");
    EXPECT_EQ(invalid_key, nullptr);

    arr->release();
}

/*!
 * \brief 测试WTSVariant成员名称获取
 */
TEST_F(WTSVariantTest, TestMemberNames) {
    WTSVariant* obj = WTSVariant::createObject();
    ASSERT_NE(obj, nullptr);

    // 添加一些成员
    obj->append("name", "test");
    obj->append("value", 42);
    obj->append("active", true);

    // 获取成员名称列表
    WTSVariant::MemberNames names = obj->memberNames();
    EXPECT_EQ(names.size(), 3u);

    // 验证成员名称存在（顺序可能不同）
    bool found_name = false, found_value = false, found_active = false;
    for (const auto& name : names) {
        if (name == "name") found_name = true;
        else if (name == "value") found_value = true;
        else if (name == "active") found_active = true;
    }
    
    EXPECT_TRUE(found_name);
    EXPECT_TRUE(found_value);
    EXPECT_TRUE(found_active);

    obj->release();
}

/*!
 * \brief 测试WTSVariant数组和对象的空状态
 */
TEST_F(WTSVariantTest, TestEmptyContainers) {
    // 测试空对象
    WTSVariant* empty_obj = WTSVariant::createObject();
    ASSERT_NE(empty_obj, nullptr);
    EXPECT_TRUE(empty_obj->isObject());
    EXPECT_EQ(empty_obj->size(), 0u);
    EXPECT_FALSE(empty_obj->has("anything"));
    
    WTSVariant::MemberNames empty_names = empty_obj->memberNames();
    EXPECT_EQ(empty_names.size(), 0u);
    
    empty_obj->release();

    // 测试空数组
    WTSVariant* empty_arr = WTSVariant::createArray();
    ASSERT_NE(empty_arr, nullptr);
    EXPECT_TRUE(empty_arr->isArray());
    EXPECT_EQ(empty_arr->size(), 0u);
    EXPECT_EQ(empty_arr->get(static_cast<uint32_t>(0)), nullptr);
    
    empty_arr->release();
}

/*!
 * \brief 测试WTSVariant字符串处理
 */
TEST_F(WTSVariantTest, TestStringHandling) {
    WTSVariant* obj = WTSVariant::createObject();
    ASSERT_NE(obj, nullptr);

    // 测试普通字符串
    obj->append("normal", "Hello World");
    EXPECT_EQ(obj->getString("normal"), "Hello World");

    // 测试空字符串
    obj->append("empty", "");
    EXPECT_EQ(obj->getString("empty"), "");

    // 测试中文字符串
    obj->append("chinese", "测试中文");
    EXPECT_EQ(obj->getString("chinese"), "测试中文");

    // 测试特殊字符
    obj->append("special", "!@#$%^&*()");
    EXPECT_EQ(obj->getString("special"), "!@#$%^&*()");

    obj->release();
}

/*!
 * \brief 测试WTSVariant数值类型处理
 */
TEST_F(WTSVariantTest, TestNumericTypes) {
    WTSVariant* obj = WTSVariant::createObject();
    ASSERT_NE(obj, nullptr);

    // 测试32位整数
    obj->append("int32_pos", 12345);
    obj->append("int32_neg", -12345);
    EXPECT_EQ(obj->getInt32("int32_pos"), 12345);
    EXPECT_EQ(obj->getInt32("int32_neg"), -12345);

    // 测试32位无符号整数
    obj->append("uint32_val", 4294967295u);
    EXPECT_EQ(obj->getUInt32("uint32_val"), 4294967295u);

    // 测试64位整数
    obj->append("int64_val", 1234567890123456789LL);
    EXPECT_EQ(obj->getInt64("int64_val"), 1234567890123456789LL);

    // 测试64位无符号整数
    obj->append("uint64_val", 18446744073709551615ULL);
    EXPECT_EQ(obj->getUInt64("uint64_val"), 18446744073709551615ULL);

    // 测试浮点数（考虑精度损失）
    obj->append("double_val", 3.141592653589793);
    EXPECT_NEAR(obj->getDouble("double_val"), 3.141592653589793, 1e-6);

    obj->release();
}

/*!
 * \brief 测试WTSVariant布尔类型处理
 */
TEST_F(WTSVariantTest, TestBooleanHandling) {
    WTSVariant* obj = WTSVariant::createObject();
    ASSERT_NE(obj, nullptr);

    // 测试true值
    obj->append("bool_true", true);
    EXPECT_TRUE(obj->getBoolean("bool_true"));
    EXPECT_EQ(obj->getString("bool_true"), "true");

    // 测试false值
    obj->append("bool_false", false);
    EXPECT_FALSE(obj->getBoolean("bool_false"));
    EXPECT_EQ(obj->getString("bool_false"), "false");

    obj->release();
} 